<?php
/**
 * ShareController.class.php
 * 用户分享数据控制器
 *
 * @author  王中艺
 * @date    2016-11-20
 */

namespace Wechat\Controller;

class ShareController extends BaseController {
    public $iInfo, $wechat;

    function _initialize(){
        parse_str(base64_decode(I('get.param')), $this->iInfo);
    }

    /**
     * 分享毕业证
     * @param user_id
     * @param course_id
     * @return json
     */
    function graduate(){
        $this->unEmptyParam(array('user_id', 'course_id'));
        $userId     = $this->iInfo['user_id'];
        $courseId   = $this->iInfo['course_id'];

        $qrCode     = D('User')->getQRCode($userId);
        $courseInfo     = M('t_course', null)->where('id = %d', $courseId)->cache(true, 300)->find();

        $this->outOInfo(array('qr_code' => $qrCode, 'course_info' => $courseInfo));
    }

    /**
     * 分享课程
     * @param   user_id
     * @param   course_id
     * @return json
     */
    function course(){
        $this->unEmptyParam(array('user_id', 'course_id'));
        $userId     = $this->iInfo['user_id'];
        $courseId   = $this->iInfo['course_id'];
        
        $userInfo   = true;
        $qrCode     = D('User')->getQRCode($userId, $userInfo);
        $courseInfo     = M('t_course a', null)
            ->join('left join t_teacher b on a.teacher_id = b.id')
            ->where('a.id = %d', $courseId)
            ->cache(true)
            ->field('a.name, a.main_title, a.sub_title, a.banner, a.decipt, b.name teacher_name')
            ->find();

        $this->outOInfo(array(
            'qr_code' => $qrCode, 
            'course_info' => $courseInfo, 
            'user_info' => $userInfo
        ));
    }

    /**
     * 课程分享图片
     * @param   user_id 用户ID
     * @param   course_id 课程ID
     * @return   PNG图片
     */
    function courseSharePNG(){
        $this->unEmptyParam(array('course_id', 'user_id'));
        $userId     = $this->iInfo['user_id'];
        $courseId   = $this->iInfo['course_id'];
        $themeId    = $this->iInfo['theme_id'] ? $this->iInfo['theme_id'] : 1;
        // $userId     = 1;
        // $courseId   = 25;
        // $themeId  = 4;
        $Image  = new \Wechat\Util\Image();
        $Image->createCourseShare($userId, $courseId, $themeId);
    }

    /**
     * 课程毕业证图片
     * @param   user_id 用户ID
     * @param   course_id 课程ID
     * @return   PNG图片
     */
    function courseGraduatePNG(){
        $this->unEmptyParam(array('user_id', 'course_id'));
        $userId     = $this->iInfo['user_id'];
        $courseId   = $this->iInfo['course_id'];
        $themeId    = $this->iInfo['theme_id'] ? $this->iInfo['theme_id'] : 1;

        // $userId     = 1;
        // $courseId   = 25;

        $Image  = new \Wechat\Util\Image();
        $Image->createCourseGraduate($userId, $courseId, $themeId);
    }

    /**
     * 分享成功用户积分添加
     * @param  user_id
     * @return [type] [description]
     */
    function shareSuccess(){
        $this->unEmptyParam(array('user_id'));
        $userId     = $this->iInfo['user_id'];
        $courseId   = $this->iInfo['course_id'];
        //分享动作记录
        $logInfo    = array(
            'user_id'   => $userId,
            'tar_type'  => 1,
            'tar_id'    => $courseId,
            'create_time'   => time()
        );
        M('t_log_share', null)->add($logInfo);

        //分享积分获取
        $score  = 3;
        $ret    = D('Score')->addScore($userId, 3, date('Ymd'), $score, '成功分享给好友');
        if($ret === false)
            $this->outOInfo($ret->errMsg, 0);

        $this->outOInfo('oh yes!');
    }

    /**
     * 分享幸福语录图片
     * @param   user_id
     * @param   motto_id
     * @return  image
     */
    function mottoImage(){
        $this->unEmptyParam(['user_id', 'motto_id']);

        $userId     = $this->iInfo['user_id'];
        $mottoId    = $this->iInfo['motto_id'];
        // echo $userId;exit;
        $Image  = new \Wechat\Util\Image();
        $Image->createMottoImg($userId, $mottoId);
    }

    /**
     * 活动相册
     * @param   $user_id
     * @param   $activity_id
     * @return  $list
     */
    function activityPicture(){
        $this->unEmptyParam(['user_id', 'activity_id']);

        $userId     = $this->iInfo['user_id'];
        $activityId     = $this->iInfo['activity_id'];

        $list   = D('Activity')->getPictureList($userId, $activityId);
        $this->outOInfo(['list' => $list]);
    }
}